package org.ResearchProject.exception;



import org.ResearchProject.dto.Result;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;

/**
 * 自定义异常，不让出错码返回500
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public Result handler(Exception e, HttpServletResponse response){
        e.printStackTrace();
        response.setStatus(402);
        return Result.error(StringUtils.hasLength(e.getMessage()) ? e.getMessage(): "服务器异常");
    }
}
